package com.d.task;

import com.d.business.domain.DingProcessRecordsDO;
import com.d.business.domain.dto.BDingTalkDTO;
import com.d.business.service.BDingTalkService;
import com.d.business.service.DingProcessRecordsService;
import com.d.ydfactory.constants.ApproveConstant;
import com.d.ydfactory.YdApproveFactory;
import com.d.ydfactory.domain.ApproveStartResult;
import com.d.ydfactory.handle.ApproveEventHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

/**
 * 宜搭审批任务处理
 */
@Component
public class YdApprovalTask {

    @Autowired
    private ApproveEventHandle approveEventHandle;

    @Autowired
    private DingProcessRecordsService dingProcessRecordsService;

    @Autowired
    private BDingTalkService bDingTalkService;

    @Value("${system.serviceNo}")
    private Integer serviceNo;

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 审批任务
     */
//    @Scheduled(cron = "0 0/2 * * * ?")
    public void StorageApprovalTask() {
        if (serviceNo != 1)
            return;
//        if (!active.equals("pro"))
//            return;

        List<BDingTalkDTO> dingTalkDTOS = bDingTalkService.getListByTypeAndValid(null);

        for(BDingTalkDTO ding:dingTalkDTOS) {
            BDingTalkDTO dto = bDingTalkService.getById(ding.getId());
            if (dto.getStatus() == 0) {
                //控制测试环境可发起的流程
                if (!active.equals("pro")) {
                    if (ding.getType() != 1)
                        continue;
                }
                YdApproveFactory factory = approveEventHandle.factory(ding.getType());
                if (factory != null) {
                    ApproveStartResult result = factory.start(ding.getSourceId());

                    DingProcessRecordsDO recordsDO = new DingProcessRecordsDO();
                    recordsDO.setBusinessType(ApproveConstant.yidaProcessConfMap.get(result.getFromUuid()));
                    recordsDO.setBusinessNo(result.getBusinessNo());
                    recordsDO.setStaffId(result.getStaffId());
                    recordsDO.setCreatedTime(new Date());
                    recordsDO.setFinishTime(new Date());
                    Integer status = 1;
                    if (result.getCode().equals("0")) {
                        recordsDO.setInstanceStatus("START");
                        recordsDO.setInstanceId(result.getInstanceId());
                    } else {
                        status = 0;
                        recordsDO.setInstanceStatus("ERROR");
                        recordsDO.setRemark(result.getMsg());
                    }
                    if (result.getSourceIds() != null && result.getSourceIds().length() > 0) {

                        bDingTalkService.updateDingTalkStatusBySourceIdsAndType(result.getSourceIds(), ding.getType(), status, result.getMsg(), result.getInstanceId());
                    } else {
                        bDingTalkService.updateDingTalkStatus(ding.getId(), status, result.getMsg(), result.getInstanceId());
                    }
                    dingProcessRecordsService.save(recordsDO);
                }
            }
        }
    }
}
